package com.yuanheng100.ems4j.service;

import com.alibaba.fastjson.JSON;
import com.yuanheng100.ems4j.api.IApplicationService;
import com.yuanheng100.ems4j.common.constant.ApplicationStatus;
import com.yuanheng100.ems4j.mapper.Ems4jApplicationMapper;
import com.yuanheng100.ems4j.model.Ems4jApplication;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by wangguangshuo on 2016/12/19.
 */
@Service
public class ApplicationServiceImpl implements IApplicationService
{
    private static Logger logger = LoggerFactory.getLogger(ApplicationServiceImpl.class);

    @Autowired
    private Ems4jApplicationMapper ems4jApplicationMapper;

    @Override
    public List<Ems4jApplication> getApplicationList()
    {
        return ems4jApplicationMapper.selectAll();
    }

    @Override
    public Ems4jApplication getApplicationById(int appID)
    {
        return ems4jApplicationMapper.selectByPrimaryKey(appID);
    }

    @Override
    public int addApplication(Ems4jApplication newApplication)
    {
        Ems4jApplication ems4jApplication = ems4jApplicationMapper.selectByPrimaryKey(newApplication.getAppid());
        if (ems4jApplication != null)
        {
            return 0;
        }
        newApplication.setAppstatus(ApplicationStatus.NORMAL.getCode());
        logger.info("新增应用：" + JSON.toJSONString(newApplication));
        return ems4jApplicationMapper.insert(newApplication);
    }

    @Override
    public int updateApplication(Ems4jApplication newApplication)
    {
        logger.info("修改应用：" + JSON.toJSONString(newApplication));
        return ems4jApplicationMapper.updateByPrimaryKey(newApplication);
    }

    @Override
    public int deleteApplication(int appid)
    {
        Ems4jApplication ems4jApplication = ems4jApplicationMapper.selectByPrimaryKey(appid);
        if (ems4jApplication == null)
        {
            return 0;
        }
        ems4jApplication.setAppstatus(ApplicationStatus.DELETE.getCode());
        logger.info("删除应用：" + JSON.toJSONString(ems4jApplication));
        return ems4jApplicationMapper.updateByPrimaryKey(ems4jApplication);
    }

}
